package org.bp.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author baiping
 * @version 1.0
 * @date 2017/9/6
 */
public class FileUtils {

	/**
	 * 往文件中追加内容
	 * 
	 * @param file
	 * @param str
	 * @return
	 */
	public static boolean appendLineToFile(File file, String str) {
		FileWriter writer = null;
		boolean e1;
		try {
			writer = new FileWriter(file, true);
			writer.write(str + System.lineSeparator());
			boolean e = true;
			return e;
		} catch (Exception var14) {
			var14.printStackTrace();
			e1 = false;
		} finally {
			StreamUtils.close(writer);
		}
		return e1;
	}

	/**
	 * 往文件中写文件
	 * 
	 * @param file
	 * @param str
	 * @param encoding
	 * @return
	 */
	public static boolean writeToFile(File file, String str, String encoding) {
		BufferedWriter writer = null;
		boolean e1;
		try {
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));
			writer.write(str);
			boolean e = true;
			return e;
		} catch (Exception var15) {
			var15.printStackTrace();
			e1 = false;
		} finally {
			StreamUtils.close(writer);
		}
		return e1;
	}

	/**
	 * 按行读取
	 * 
	 * @param path
	 * @param encoding
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<String> loadTextInLineFromFile(File path, String encoding) {
		FileInputStream fileInputStream = null;
		Object e1;
		try {
			fileInputStream = new FileInputStream(path);
			List<String> e = loadTextInLineFromStream(fileInputStream, encoding);
			return e;
		} catch (Exception var14) {
			e1 = null;
			var14.printStackTrace();
		} finally {
			StreamUtils.close(fileInputStream);
		}
		return (List<String>) e1;
	}

	/**
	 * 按行读取
	 * 
	 * @param stream
	 * @param encoding
	 * @return
	 */
	public static List<String> loadTextInLineFromStream(InputStream stream, String encoding) {
		try {
			BufferedReader e = new BufferedReader(new InputStreamReader(stream, encoding));

			try {
				ArrayList<String> lst = new ArrayList<>();

				String line;
				while ((line = e.readLine()) != null) {
					lst.add(line);
				}

				ArrayList<String> var5 = lst;
				return var5;
			} finally {
				e.close();
			}
		} catch (Exception var10) {
			var10.printStackTrace();
			return null;
		}
	}
	public static void visit(File file){
		file.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				File file = new File(dir, name);
				if(file.isDirectory()){
					visit(file);
				}
				if(file.isFile()){
					//你的操作
					return true;
				}
				return false;
			}
		});
	}
	/**
	 * 读取文件内容
	 * 
	 * @param path
	 * @param encoding
	 * @return
	 */
	public static String loadTextFile(File path, String encoding) {
		FileInputStream fileInputStream = null;

		try {
			fileInputStream = new FileInputStream(path);
			String e = loadTextFromStream(fileInputStream, encoding);
			return e;
		} catch (Exception var14) {
			var14.printStackTrace();
		} finally {
			StreamUtils.close(fileInputStream);
		}

		return null;
	}

	public static String loadTextFromStream(InputStream stream, String encoding) {
		List<String> lines = loadTextInLineFromStream(stream, encoding);
		if (lines == null) {
			return null;
		} else {
			StringBuilder str = new StringBuilder();
			Iterator<String> var4 = lines.iterator();

			while (var4.hasNext()) {
				String line = (String) var4.next();
				str.append(line).append("\n");
			}

			return str.toString();
		}
	}

	/**
	 * 复制文件
	 * 
	 * @param srcPath
	 * @param destPath
	 * @return
	 */
	public static boolean copyFile(String srcPath, String destPath) {
		File srcFile = new File(srcPath);
		File destFile = new File(destPath);
		createFolders(destFile.getParent());
		FileInputStream is = null;
		FileOutputStream os = null;

		boolean count;
		try {
			is = new FileInputStream(srcFile);
			os = new FileOutputStream(destFile);
			byte[] e = new byte[1024];
			count = false;

			int count1;
			while ((count1 = is.read(e)) != -1) {
				os.write(e, 0, count1);
			}

			boolean e1 = true;
			return e1;
		} catch (FileNotFoundException var25) {
			var25.printStackTrace();
			count = false;
			return count;
		} catch (Exception var26) {
			count = false;
		} finally {
			StreamUtils.close(os, is);
		}

		return count;
	}

	/**
	 * 创建文件夹
	 * 
	 * @param folderPath
	 * @return
	 */
	public static boolean createFolders(String folderPath) {
		File myFilePath = new File(folderPath);

		try {
			return !myFilePath.exists() ? myFilePath.mkdirs() : true;
		} catch (Exception var4) {
			var4.printStackTrace();
			return false;
		}
	}

	/**
	 * 获取文件扩展名 如 .txt
	 * 
	 * @param fileName
	 * @return
	 */
	public static String getExtName(String fileName) {
		int pos = fileName.lastIndexOf(46);
		return pos == -1 ? fileName : fileName.substring(pos, fileName.length());
	}

	/**
	 * 删除文件扩展名
	 * 
	 * @param fileName
	 * @return
	 */
	public static String delExtName(String fileName) {
		int pos = fileName.lastIndexOf(46);
		return pos == -1 ? fileName : fileName.substring(0, pos);
	}

	/**
	 * 将win路径转换成linux路径
	 * 
	 * @param path
	 * @return
	 */
	public static String linuxPath(String path) {
		return path.replace("\\", "/");
	}
}
